package com.aidong.web.controller.customer;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.cache.NameItem;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CustomerComplaintAuditDao;
import com.aidong.dao.wills.CustomerComplaintDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberEarnestDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.CustomerComplaint;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtContract;
import com.aidong.model.wills.Upload;
import com.aidong.service.UploadService;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"customerComplaint"})
public class CustomerComplaintController extends BaseController {
    @Resource
    CustomerComplaintDao customerComplaintDao;
    @Resource
    CustomerComplaintAuditDao customerComplaintAuditDao;
    @Resource
    UploadService uploadService;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    MemberEarnestDao memberEarnestDao;

    public CustomerComplaintController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("customer/complaint/list");
        mv.addObject("complaintStatus", this.getTypeList("complaintStatus"));
        mv.addObject("complaintType", this.getTypeList("complaintType"));
        mv.addObject("complaintPriority", this.getTypeList("complaintPriority"));
        mv.addObject("complaintProcType", this.getTypeList("complaintProcType"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "title",required = false) String title, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "procType",required = false) Integer procType, @RequestParam(value = "stopDay",required = false) String stopDay, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.type", type);
        cd.eq("c.procType", procType);
        if(!this.chkNull(status)) {
            cd.eq("c.status", status);
        } else {
            cd.gt("c.status", Integer.valueOf(0));
        }

        if(!this.chkNull(title)) {
            cd.like("title", "%" + title + "%");
        }

        if(!this.chkNull(name)) {
            cd.like("i.chName", "%" + name + "%");
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("c.complainTime", this.getDateFrom(dateFrom));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("c.complainTime", this.getDateTo(dateTo));
        }

        if(!this.chkNull(stopDay)) {
            String[] mv = stopDay.split(",");
            if(mv.length == 2) {
                Integer n1 = this.parseInt(mv[0]);
                Integer n2 = this.parseInt(mv[1]);
                if(!this.chkNullZ(n1) || !this.chkNullZ(n2)) {
                    String s = "";
                    if(!this.chkNullZ(n1)) {
                        s = "c.updateTime<getdate()-" + n1;
                    }

                    if(!this.chkNullZ(n2)) {
                        s = s + (s.length() > 0?" and ":"") + "c.updateTime>getdate()-" + n2;
                    }

                    cd.addSqlw("c.status not in (9,10,11) and " + s, new Object[0]);
                }
            }
        }

        this.customerComplaintDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("customer/complaint/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "title",required = false) String title, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "procType",required = false) Integer procType, @RequestParam(value = "stopDay",required = false) String stopDay, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "c.shopCode", shopCode);
            cd.eq("c.type", type);
            cd.eq("c.procType", procType);
            if(!this.chkNull(status)) {
                cd.eq("c.status", status);
            } else {
                cd.gt("c.status", Integer.valueOf(0));
            }

            if(!this.chkNull(title)) {
                cd.like("title", "%" + title + "%");
            }

            if(!this.chkNull(name)) {
                cd.like("i.chName", "%" + name + "%");
            }

            if(!this.chkNull(dateFrom)) {
                cd.ge("c.complainTime", this.getDateFrom(dateFrom));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("c.complainTime", this.getDateTo(dateTo));
            }

            if(!this.chkNull(stopDay)) {
                String[] xls = stopDay.split(",");
                if(xls.length == 2) {
                    Integer sdf = this.parseInt(xls[0]);
                    Integer sdf2 = this.parseInt(xls[1]);
                    if(!this.chkNullZ(sdf) || !this.chkNullZ(sdf2)) {
                        String i$ = "";
                        if(!this.chkNullZ(sdf)) {
                            i$ = "c.updateTime<getdate()-" + sdf;
                        }

                        if(!this.chkNullZ(sdf2)) {
                            i$ = i$ + (i$.length() > 0?" and ":"") + "c.updateTime>getdate()-" + sdf2;
                        }

                        cd.addSqlw("c.status not in (9,10,11) and " + i$, new Object[0]);
                    }
                }
            }

            this.customerComplaintDao.queryPageList(e, cd);
            ExportXls xls1 = new ExportXls(response, "客诉列表", "客诉列表");
            xls1.addHead(new String[]{"门店编号", "门店名称", "会员姓名", "会员号", "会员卡号", "电话", "标题", "分类", "紧急程度", "处理状态", "诉求详情", "处理分类", "处理结果", "处理备注", "投诉原因", "风险金扣除", "提交时间", "更新时间", "操作员"});
            xls1.setWidth(new int[]{10, 10, 10, 20, 10, 12, 30, 10, 10, 15, 30, 15, 20, 20, 15, 20, 20, 20, 15});
            new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf21 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$1 = e.getResultList().iterator();

            while(i$1.hasNext()) {
                CustomerComplaint complaint = (CustomerComplaint)i$1.next();
                xls1.addRow(new Object[]{complaint.getShopCode(), this.cache.getValue("shopName", complaint.getShopCode()), complaint.getMemberName(), complaint.getMemberCode(), complaint.getCardCode(), complaint.getMobile(), complaint.getTitle(), this.cache.getValue("complaintType", String.valueOf(complaint.getType())), this.cache.getValue("complaintPriority", complaint.getPriority()), this.cache.getValue("complaintStatus", String.valueOf(complaint.getStatus())), complaint.getContent(), this.cache.getValue("complaintProcType", complaint.getProcType()), complaint.getProcResult(), complaint.getProcNote(), this.cache.getValue("complainReason", complaint.getComplainReason()), this.nvl(complaint.getRiskMoney()), complaint.getComplainTime() != null?sdf21.format(complaint.getComplainTime()):"", complaint.getUpdateTime() != null?sdf21.format(complaint.getUpdateTime()):"", this.nvl(complaint.getCreateUser()) + " - " + this.nvl(this.cache.getValue("userName", complaint.getCreateUser()))});
            }

            xls1.close();
            this.addUserLog("导出客诉列表", e.getResultList().size() + "条记录");
        } catch (Exception var18) {
            this.log.error("import", var18);
        }

        return null;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        CustomerComplaint complaint = new CustomerComplaint();
        complaint.setShopCode(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("customer/complaint/add");
        ArrayList complaintPriority = new ArrayList();
        List ls = this.getTypeList("complaintPriority");
        Iterator i$ = ls.iterator();

        while(i$.hasNext()) {
            NameItem item = (NameItem)i$.next();
            if(item.getId().compareTo("3") > 0) {
                complaintPriority.add(item);
            }
        }

        mv.addObject("complaint", complaint);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("complaintType", this.getTypeList("complaintType"));
        mv.addObject("complaintPriority", complaintPriority);
        mv.addObject("complaintProcType", this.getTypeList("complaintProcType"));
        mv.addObject("complainReason", this.getTypeList("complainReason"));
        mv.addObject("_picPath", this.getSetting("pic.path"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("complaint") CustomerComplaint complaint) {
        if(this.chkNull(complaint.getMemberCode()) && this.chkNullZ(complaint.getEarnestId())) {
            return this.getResult(false, "请输入会员编号或定金号");
        } else if(this.chkNull(complaint.getTitle())) {
            return this.getResult(false, "请输入标题");
        } else if(this.chkNull(complaint.getContent())) {
            return this.getResult(false, "请输入内容");
        } else {
            if(this.chkNull(complaint.getStatus())) {
                complaint.setStatus(Integer.valueOf(1));
            }

            complaint.setComplainTime(new Date());
            complaint.setStatus(Integer.valueOf(2));
            complaint.setCreateUser(this.getLoginUserCode());
            complaint.setCreateTime(new Date());
            complaint.setUpdateTime(new Date());

            try {
                this.customerComplaintDao.add(complaint);
                this.addUserLog("新增客诉", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
            } catch (Exception var3) {
                this.log.error("add complaint", var3);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"getMember"})
    @ResponseBody
    public String getMember(@RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "earnestId",required = false) Integer earnestId) {
        if(this.chkNull(memberCode) && this.chkNull(earnestId)) {
            return this.getResult(false, "会员编号错误");
        } else {
            MemberEarnest earnest = null;
            StringBuilder sb = new StringBuilder();
            if(this.chkNull(memberCode)) {
                earnest = (MemberEarnest)this.memberEarnestDao.getById(earnestId);
                if(earnest == null) {
                    return this.getResult(false, "没有这个定金");
                }

                memberCode = earnest.getMemberCode();
                if(this.chkNull(memberCode)) {
                    sb.append("{memberCode:\"").append(this.nvl(earnest.getMemberCode())).append("\"").append(",earnestId:\"").append(earnest.getSaleId()).append("\"").append(",cardCode:\"").append(this.nvl(earnest.getCardCode())).append("\"").append(",shopCode:\"").append(earnest.getShopCode()).append("\"").append(",shopName:\"").append(this.cache.getValue("shopName", earnest.getShopCode())).append("\"").append(",memberName:\"").append(this.nvl(earnest.getCustName())).append("\"").append(",gender:\"").append(this.nvl(earnest.getGender())).append("\"").append(",idType:\"").append(this.nvl(earnest.getIdType())).append("\"").append(",idTypeName:\"").append(this.nvl(this.cache.getValue("idType", earnest.getIdType()))).append("\"").append(",idNumber:\"").append(this.nvl(earnest.getIdNumber())).append("\"").append(",mobile:\"").append(this.nvl(earnest.getMobile())).append("\"").append(",contract:[]}");
                    return this.getSuccess(sb.toString(), "");
                }
            }

            MemberInfo member = this.memberInfoDao.getByMemberCode(memberCode);
            if(member == null) {
                return this.getResult(false, "会员编号错误");
            } else {
                List list1 = this.memberContractDao.getListByMemberCode(memberCode);
                List list2 = this.ptContractDao.getListByMemberCode(memberCode);
                sb.append("{memberCode:\"").append(member.getMemberCode()).append("\"").append(",earnestId:\"").append(earnest == null?"":earnest.getSaleId()).append("\"").append(",cardCode:\"").append(member.getCardCode()).append("\"").append(",shopCode:\"").append(member.getShopCode()).append("\"").append(",shopName:\"").append(this.cache.getValue("shopName", member.getShopCode())).append("\"").append(",memberName:\"").append(member.getChName()).append("\"").append(",gender:\"").append(member.getGender()).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idTypeName:\"").append(this.cache.getValue("idType", member.getIdType())).append("\"").append(",idNumber:\"").append(member.getIdNumber()).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",contract:[");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                DecimalFormat nf = new DecimalFormat("0.00");
                int n = 0;

                Iterator i$;
                MemberContract contract;
                for(i$ = list1.iterator(); i$.hasNext(); sb.append("\"").append(contract.getShopCode() + "-" + this.cache.getValue("shopName", contract.getShopCode())).append(",").append(contract.getContractCode()).append(",会籍,").append(this.cache.getValue("memberSaleType", contract.getSaleType())).append(",").append(this.cache.getValue("cardType", contract.getCardType())).append(",").append(nf.format(contract.getFactPrice())).append(",").append(sdf.format(contract.getBeginTime())).append(",").append(sdf.format(contract.getEndTime())).append(",").append(contract.getTotalCount().intValue() > 0?contract.getRestCount():"-").append("\"")) {
                    contract = (MemberContract)i$.next();
                    if(n++ > 0) {
                        sb.append(",");
                    }
                }

                PtContract var13;
                for(i$ = list2.iterator(); i$.hasNext(); sb.append("\"").append(var13.getShopCode() + "-" + this.cache.getValue("shopName", var13.getShopCode())).append(",").append(var13.getContractCode()).append(",私教,").append(this.cache.getValue("ptSaleType", var13.getSaleType())).append(",").append(this.cache.getValue("ptItemName", var13.getPtCode())).append(",").append(nf.format(var13.getFactPrice())).append(",").append(sdf.format(var13.getBeginTime())).append(",").append(sdf.format(var13.getEndTime())).append(",").append(var13.getQuantity().intValue() - var13.getBespeakQuantity().intValue()).append("\"")) {
                    var13 = (PtContract)i$.next();
                    if(n++ > 0) {
                        sb.append(",");
                    }
                }

                sb.append("]}");
                return this.getSuccess(sb.toString(), "");
            }
        }
    }

    @RequestMapping({"view"})
    public ModelAndView view(@RequestParam("id") int id) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(Integer.valueOf(id));
        if(complaint == null) {
            return this.showAlert("没有这个客诉");
        } else {
            MemberInfo member = null;
            MemberEarnest earnest = null;
            if(!this.chkNull(complaint.getMemberCode())) {
                member = this.memberInfoDao.getByMemberCode(complaint.getMemberCode());
            } else if(!this.chkNullZ(complaint.getEarnestId())) {
                earnest = (MemberEarnest)this.memberEarnestDao.getById(complaint.getEarnestId());
                if(earnest != null) {
                    member = new MemberInfo(earnest.getMemberCode());
                    member.setCardCode(earnest.getCardCode());
                    member.setChName(earnest.getCustName());
                    member.setIdType(earnest.getIdType());
                    member.setIdNumber(earnest.getIdNumber());
                    member.setMobile(earnest.getMobile());
                    member.setShopCode(earnest.getShopCode());
                }
            }

            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                if(!this.chkNull(complaint.getContent())) {
                    complaint.setContent(complaint.getContent().replaceAll("\\n", "<br>"));
                }

                ArrayList picList = null;
                String[] auditList;
                int len$;
                if(!this.chkNull(complaint.getPicUrls())) {
                    picList = new ArrayList();
                    String[] contractList = complaint.getPicUrls().split(",");
                    auditList = contractList;
                    int mv = contractList.length;

                    for(len$ = 0; len$ < mv; ++len$) {
                        String i$ = auditList[len$];
                        picList.add(i$);
                    }
                }

                ArrayList var12 = null;
                if(!this.chkNull(complaint.getContractNote())) {
                    var12 = new ArrayList();
                    auditList = complaint.getContractNote().split("\\|");
                    String[] var14 = auditList;
                    len$ = auditList.length;

                    for(int var16 = 0; var16 < len$; ++var16) {
                        String s = var14[var16];
                        var12.add(s.split(","));
                    }
                }

                List var13 = this.customerComplaintAuditDao.getListByComplaintId(id);
                ModelAndView var15 = new ModelAndView("customer/complaint/view");
                var15.addObject("complaint", complaint);
                var15.addObject("member", member);
                if(earnest != null) {
                    var15.addObject("earnest", earnest);
                }

                var15.addObject("picList", picList);
                var15.addObject("contractList", var12);
                var15.addObject("auditList", var13);
                var15.addObject("_picPath", this.getSetting("pic.path"));
                var15.addObject("_oper", "edit");
                return var15;
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("id") int id) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(Integer.valueOf(id));
        if(complaint == null) {
            return this.showAlert("没有这个客诉");
        } else {
            ArrayList picList = null;
            if(!this.chkNull(complaint.getPicUrls())) {
                picList = new ArrayList();
                String[] mv = complaint.getPicUrls().split(",");
                String[] arr$ = mv;
                int len$ = mv.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    String s = arr$[i$];
                    picList.add(s);
                }
            }

            ModelAndView var9 = new ModelAndView("customer/complaint/edit");
            var9.addObject("complaint", complaint);
            var9.addObject("picList", picList);
            var9.addObject("complainReason", this.getTypeList("complainReason"));
            var9.addObject("_picPath", this.getSetting("pic.path"));
            var9.addObject("_oper", "edit");
            return var9;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("complaint") CustomerComplaint complaint) {
        if(this.chkNull(complaint.getTitle())) {
            return this.getResult(false, "请输入标题");
        } else if(this.chkNull(complaint.getContent())) {
            return this.getResult(false, "请输入内容");
        } else {
            if(this.chkNull(complaint.getStatus())) {
                complaint.setStatus(Integer.valueOf(1));
            }

            CustomerComplaint complaint1 = (CustomerComplaint)this.customerComplaintDao.getById(complaint.getComplainId());
            if(complaint1 == null) {
                return this.getResult(false, "没有这个客诉");
            } else {
                complaint1.setTitle(complaint.getTitle());
                complaint1.setContent(complaint.getContent());
                complaint1.setStatus(complaint.getStatus());
                complaint1.setComplainReason(complaint.getComplainReason());
                complaint1.setRiskMoney(complaint.getRiskMoney());
                complaint1.setUpdateTime(new Date());

                try {
                    this.customerComplaintDao.update(complaint1);
                    this.addUserLog("修改客诉", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
                } catch (Exception var4) {
                    this.log.error("edit complaint", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("msgId") int msgId) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(Integer.valueOf(msgId));
        if(complaint == null) {
            return this.getResult(false, "没有这个客诉");
        } else {
            try {
                this.customerComplaintDao.updateStatus(msgId, 0);
                this.addUserLog("删除客诉", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
            } catch (Exception var4) {
                this.log.error("del complaint", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"_upload"})
    @ResponseBody
    public String _upload(@RequestParam("file") MultipartFile file) {
        StringBuilder msg = new StringBuilder();
        Upload upload = this.uploadService.upload(file, msg);
        return upload == null?this.getResult(false, msg.toString()):this.getSuccess("{\"fid\":" + upload.getFileId() + ",\"furl\":\"" + upload.getUrl() + "\"}", "上传成功");
    }
}
